#include "IfElseIfFunctionNode.h"
#include "..\Common\AssertHelper.h"

using namespace std;

namespace CExpression
{
	void IfElseIfFunctionNode::Eval(ExecuteContext &context)
	{
		for(vector< WrapperPtr<Node> >::size_type i=this->parameters.size()-1;i>=0;i-=2)
		{
			(this->parameters)[i]->Eval(context);
			if(AssertHelper::ExpectType(context.TempResultType(),ExecuteRunTimeType::Boolean,context,TEXT("IfElseIf function's condition parameter must be bool type.")))
			{
				if(context.GlobalTempBoolean())
				{
					(this->parameters)[i-1]->Eval(context);
					break;
				}
			}
			else
			{
				break;
			}
		}
	}

	shared_ptr<Node> IfElseIfFunctionNode::Clone(Expression &parent)
	{
		shared_ptr<Node> result(new IfElseIfFunctionNode());
		IfElseIfFunctionNode *node=static_cast<IfElseIfFunctionNode*>(result.get());
		for(vector< shared_ptr<Node> >::iterator i=this->parameters.begin();i!=this->parameters.end();++i)
		{
			node->AddParameter((*i)->Clone(parent));
		}
		return result;
	}
}